package com.muy.redis.view.window.redis.view.table;

import com.intellij.openapi.project.Project;
import com.intellij.ui.JBColor;
import com.intellij.ui.RoundedLineBorder;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.ui.JBUI;
import com.muy.redis.utils.GuiUtils;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;
import javax.swing.tree.TreeCellEditor;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.EventObject;

/**
 * @Author jiyanghuang
 * @Date 2022/8/28 00:12
 */
public class CellEditorText extends JBPanel implements TableCellEditor, TreeCellEditor {

    private final JTextField textField;
    private final JLabel button;
    Border BUTTON_OUTSIDE_BORDER = JBUI.Borders.empty(1);
    Border BUTTON_INSIDE_BORDER = JBUI.Borders.empty(0, 8);
    Color BUTTON_BORDER_COLOR = new JBColor(new Color(0x8C8C8C), new Color(0x606060));
    RoundedLineBorder BUTTON_LINE_BORDER = new RoundedLineBorder(BUTTON_BORDER_COLOR, 4);
    CompoundBorder BUTTON_BORDER = new CompoundBorder(BUTTON_OUTSIDE_BORDER, new CompoundBorder(BUTTON_LINE_BORDER, BUTTON_INSIDE_BORDER));

    private Project project;
    private boolean[] editorStatus = {true};

    public CellEditorText(Project project){

        this.project = project;
        // 一定要设置 borderLayout, 否则框不会匹配
        setLayout(new BorderLayout(0, 0));
        setBounds(0, 0, 0, 0);
        withBorder(JBUI.Borders.empty(0));
        textField = new JBTextField();
        textField.setMargin(JBUI.insets(0, 0, 0, 0));
        add(textField, BorderLayout.CENTER);

        button = new JLabel(GuiUtils.DATA_EDITOR_BROWSE);
        button.setBorder(BUTTON_BORDER);
        button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                com.muy.common.dialog.DialogJson dialogJson = new com.muy.common.dialog.DialogJson(project, (text) -> {
                    editorStatus[0] = true;
                }, "");
                editorStatus[0] = false;
                dialogJson.show();
            }
        });
//        textField.setPreferredSize(new Dimension(150, -1));
        textField.setEditable(false);
        String shortcutText = "this is Value";
        button.setToolTipText("Open editor (" + shortcutText + ')');
        add(button, BorderLayout.EAST);
    }


    /**
     * 被双击时展示的内容
     * @param table
     * @param value
     * @param isSelected
     * @param row
     * @param column
     * @return
     */
    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        textField.setText("this is editting");
        return this;
    }

    @Override
    public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
        return null;
    }

    /**
     * 正常显示的内容
     * @return
     */
    @Override
    public Object getCellEditorValue() {
        return "valueShow";
    }

    @Override
    public boolean isCellEditable(EventObject anEvent) {
        return true;
    }

    @Override
    public boolean shouldSelectCell(EventObject anEvent) {
        return false;
    }

    @Override
    public boolean stopCellEditing() {
//        System.out.println("editorStatus--->" + editorStatus[0]);
        return editorStatus[0];
    }

    @Override
    public void cancelCellEditing() {
        editorStatus[0] = true;
    }

    @Override
    public void addCellEditorListener(CellEditorListener l) {

    }

    @Override
    public void removeCellEditorListener(CellEditorListener l) {

    }
}
